(function(){

var eventNames = 'ontouchstart' in document ? {
	mousedown: 'touchstart',
	mousemove: 'touchmove',
	mouseup: 'touchend'
} : {
	mousedown: 'mousedown',
	mousemove: 'mousemove',
	mouseup: 'mouseup'
};

function $Q(q){
	return typeof(q) == 'string' ? document.querySelector(q) : q;
}
function fun(v){
	return typeof(v) == 'function';
}

function addEvent(el, name, callback, useCapture){
	if(name in eventNames){
		name = eventNames[name];
	}
	el.addEventListener(name, callback, useCapture);
}
function removeEvent(el, name, callback){
	if(name in eventNames){
		name = eventNames[name];
	}
	el.removeEventListener(name, callback);
}
function getSingleEvent(e){
	if('changedTouches' in e){
		return e.changedTouches[0];
	}
	return e;
}
function getSingleEventPostion(e){
	e = getSingleEvent(e);
	return [e.clientX, e.clientY];
}
function getSingleEventPostionINT(e){
	e = getSingleEvent(e);
	return [e.clientX >> 0, e.clientY >> 0];
}

/*
 用户绘制对象基类
 @q canvas标记查询字串
 */
function DocumentDrawer(){
	var _ = this;
	_.state = 0;
	_.currentPoint = null;
	_.previousPoint = null;
	_.path = [];
	_.$pointMap = function(p){ return p; };
	_.$getPoint = function(e){ return getSingleEventPostion(e); };
	_.$gotPoint = function(p){ _.path.push(p); };
	_.$bindElement = function(el){
		_.target = el;
		addEvent(el, 'mousedown', _.$mousedown = function(e){
			e.preventDefault();
			_.currentPoint  = _.$pointMap(getSingleEventPostion(e));
			_.previousPoint = _.currentPoint;
			fun(_.onbegin) && _.onbegin(_.currentPoint, _.state);
			_.state = 1;
			_.path.splice(0, _.path.length);
		});
	};
	// addEvent(document, 'mousedown', _.$mousedown = function(e){
	// 	if('button' in e && e.button != 0){
	// 		e.preventDefault();
	// 		return;
	// 	}
	// 	_.currentPoint  = _.$pointMap(getSingleEventPostion(e));
	// 	_.state = 1;
	// 	fun(_.onbegin) && _.onbegin(_.currentPoint, _.state);
	// });
	addEvent(document, 'mousemove', _.$mousemove = function(e){
		if(_.state != 1) return;
		_.currentPoint = _.$pointMap(_.$getPoint(e));
		_.$gotPoint(_.currentPoint);
		fun(_.onmove) && _.onmove(_.currentPoint, _.state);
		_.previousPoint = _.currentPoint;
	});
	addEvent(document, 'mouseup', _.$mouseup = function(e){
		if(_.state != 1) return;
		_.currentPoint = _.$pointMap(_.$getPoint(e));
		_.stop(_.currentPoint);
	});
	_.stop = function(p){
		_.state = 0;
		fun(_.onstop) && _.onstop(p || _.currentPoint, _.state);
		_.currentPoint = null;
		_.previousPoint = null;
	};
	_.dispose = function(){
		this.$mousedown && removeEvent(document, 'mousedown', this.$mousedown);
		removeEvent(document, 'mousemove', this.$mousemove);
		removeEvent(document, 'mouseup', this.$mouseup);
	};
}

/*
 用户绘制对象
 基于基类扩展
 */
function CanvasPainter(q){

	var _ = this;
	DocumentDrawer.call(_);

	var canvas = document.querySelector(q);
	_.context = canvas.getContext('2d');
	_.$pointMap = function(p){
		return { x: p[0] >> 0, y: p[1] >> 0 };
	};

	_.update = function(){
		this.context = canvas.getContext('2d');
	};
	_.update();
	_.$bindElement(canvas);
}

window.CanvasPainter = CanvasPainter;

})();